package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.CountsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<UserLike> findLoves(Integer page, Integer pagesize, Long userId) {


        Criteria criteria=Criteria.where("userId").is(userId).and("isLike").is(true);
        Query query=Query.query(criteria);
        query.skip((page-1)*pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);

        return userLikes;
    }
    @Override
    public List<UserLike> userLoves(Long id, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(id).and("isLike").is(true);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    @Override
    public List<UserLike> lovesUser(Long LikeUserId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("LikeUserId").is(LikeUserId).and("isLike").is(true);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    @Override
    public UserLike AlreadyLove(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        UserLike one = mongoTemplate.findOne(query, UserLike.class);
        return one;
    }

    @Override
    public void modifyFansLoveStatus(Long userId, Long uid) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(uid));
        UserLike one = mongoTemplate.findOne(query, UserLike.class);
//        如果不存在就新增
        if (one == null) {
            UserLike userLike = new UserLike();
            userLike.setUserId(userId);
            userLike.setLikeUserId(uid);
            userLike.setCreated(System.currentTimeMillis());
            userLike.setIsLike(true);
            userLike.setUpdated(System.currentTimeMillis());
            mongoTemplate.save(userLike);
        } else {
//            存在就更新
            Update update = Update.update("isLike", true).set("updated", System.currentTimeMillis());
            mongoTemplate.updateFirst(query, update, UserLike.class);
        }
    }

    @Override
    public void deleteLoveStatus(Long id, Long uid) {
        Query query = Query.query(Criteria.where("userId").is(id).and("LikeUserId").is(uid));
//        mongoTemplate.remove(query,UserLike.class);
        Update update = Update.update("isLike", false).set("updated", System.currentTimeMillis());
        mongoTemplate.updateFirst(query, update, UserLike.class);
    }

    @Override
    public CountsVo findCounts(Long id) { //用户的id
        CountsVo countsVo = new CountsVo();
        //查询粉丝数
        Query fans = Query.query(Criteria.where("likeUserId").is(id).and("isLike").is(true));
        Long fanscount = mongoTemplate.count(fans, UserLike.class);
        countsVo.setFanCount(fanscount.intValue());
        //查询我喜欢的数
        Query query = Query.query(Criteria.where("userId").is(id).and("isLike").is(true));
        Long usercount = mongoTemplate.count(query, UserLike.class);
        countsVo.setLoveCount(usercount.intValue());
        //查询相互喜欢数
        Query query1 = Query.query(Criteria.where("userId").is(id).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query1, UserLike.class);
        List<Long> collect = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());
        Query query2 = Query.query(Criteria.where("userId").in(collect).and("isLike").is(true).and("likeUserId").is(id));
        Long eachLoveCount = mongoTemplate.count(query2, UserLike.class);
        countsVo.setEachLoveCount(eachLoveCount.intValue());

        return countsVo;
    }
}
